package cn.com.checne.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.checne.constants.Constants;
import cn.com.checne.model.domain.UserInfo;

public class UserLoginFilter implements Filter {
	private static final long serialVersionUID = -1998907343910499L;

	public void init(FilterConfig config) {
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;

		UserInfo userInfo = (UserInfo) req.getSession().getAttribute(Constants.SESSIONNAME);
		String requestURI = req.getRequestURI();
		/**
		 * 第三个条件是：后台进程，直接抓取其他网站的信息
		 */
		if ((userInfo == null) && (requestURI.contains("/super")) && !requestURI.contains("/super/news/news-checNews.do")) {
			resp.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath() + "/manager.do");
			return;
		}
		chain.doFilter(request, response);
	}
	public void destroy() {
	}
}